package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.RemindDateUtils;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.CorePollingEquipmentMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingPlanMapper;
import com.corechina.apex_osswords.domain.entity.CorePolling;
import com.corechina.apex_osswords.domain.entity.CorePollingEquipment;
import com.corechina.apex_osswords.domain.entity.CorePollingPlan;
import com.corechina.apex_osswords.domain.vo.CorePollingVo;
import com.corechina.apex_osswords.service.ICorePollingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 巡检表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-03-09
 */
@Service
public class CorePollingServiceImpl extends ServiceImpl<CorePollingMapper, CorePolling> implements ICorePollingService {

    @Autowired
    private CorePollingMapper corePollingMapper;

    @Autowired
    private CorePollingPlanMapper corePollingPlanMapper;

    @Autowired
    private CorePollingEquipmentMapper corePollingEquipmentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response add(CorePolling corePolling) {
        if (corePolling.getBeginDate() == null || corePolling.getEndDate() == null || corePolling.getType() == null || corePolling.getFrequency() == null || corePolling.getEquipmentList() == null || corePolling.getEquipmentList().trim().equals("")) {
            return Response.error("巡查要素不全");
        }
        corePolling.setCreateTime(LocalDateTime.now());
        corePolling.setCreator(ShiroUtils.getUserId());
        corePolling.setEnterpriseId(ShiroUtils.getEnterpriseId());
        corePollingMapper.insert(corePolling);
        this.addPlanAndEquipment(corePolling);
        return Response.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response update(CorePolling corePolling) {
        corePolling.setEnterpriseId(null);
        corePolling.setCreateTime(null);
        corePolling.setCreator(null);
        if (corePolling.getBeginDate() == null || corePolling.getEndDate() == null || corePolling.getType() == null || corePolling.getFrequency() == null || corePolling.getEquipmentList() == null || corePolling.getEquipmentList().trim().equals("")) {
            return Response.error("巡查要素不全");
        }
        CorePolling dbCorePolling = corePollingMapper.selectById(corePolling.getId());
        corePollingMapper.updateById(corePolling);
        if (!corePolling.getBeginDate().isEqual(dbCorePolling.getBeginDate()) || !corePolling.getEndDate().isEqual(dbCorePolling.getEndDate())
                || !corePolling.getType().equals(dbCorePolling.getType()) || !corePolling.getFrequency().equals(dbCorePolling.getFrequency())) {
            corePollingEquipmentMapper.deleteByPollingId(corePolling.getId());
            corePollingPlanMapper.deleteByPollingId(corePolling.getId());
            this.addPlanAndEquipment(corePolling);
        } else if (!corePolling.getPlanPerson().equals(dbCorePolling.getPlanPerson())) {
            corePollingPlanMapper.updatePlanPersonByPollingId(corePolling.getPlanPerson(), corePolling.getId());
        } else if (!this.checkEquipmentList(corePolling.getEquipmentList(), dbCorePolling.getEquipmentList())) {
            corePollingEquipmentMapper.deleteByPollingId(corePolling.getId());
            String[] equipmentIds = corePolling.getEquipmentList().split(",");
            for (Integer planId : corePollingPlanMapper.getPlanListByPollingId(corePolling.getId())) {
                this.addEquipment(equipmentIds, planId);
            }
        }
        return Response.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response del(Integer id) {
        corePollingMapper.deleteById(id);
        corePollingEquipmentMapper.deleteByPollingId(id);
        corePollingPlanMapper.deleteByPollingId(id);
        return Response.ok();
    }

    @Override
    public Response getList(String name, Integer projectId, Integer planPerson, Integer type, Integer pageIndex, Integer pageSize) {
        IPage<CorePollingVo> pollingVoIPage = corePollingMapper.getList(new Page(pageIndex, pageSize), ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId(), name, projectId, planPerson, type);
        return Response.ok(pollingVoIPage.getRecords(), pollingVoIPage.getTotal());
    }

    private boolean checkEquipmentList(String equipmentList1, String equipmentList2) {
        return Arrays.equals(equipmentList1.split(","), equipmentList2.split("."));
    }

    private void addPlanAndEquipment(CorePolling corePolling) {
        List<LocalDate> remindList = RemindDateUtils.getRemindList(corePolling.getBeginDate(), corePolling.getEndDate(), corePolling.getType(), corePolling.getFrequency());
        for (LocalDate pollingTime : remindList) {
            CorePollingPlan corePollingPlan = new CorePollingPlan();
            corePollingPlan.setParentId(corePolling.getId());
            corePollingPlan.setPlanTime(pollingTime);
            corePollingPlan.setPlanPerson(corePolling.getPlanPerson());
            corePollingPlan.setStatus(0);
            corePollingPlanMapper.insert(corePollingPlan);
            this.addEquipment(corePolling.getEquipmentList().split(","), corePollingPlan.getId());
        }
    }

    @Override
    public void addEquipment(String[] equipmentIds, Integer planId) {
        for (String equipmentId : equipmentIds) {
            CorePollingEquipment corePollingEquipment = new CorePollingEquipment();
            corePollingEquipment.setEquipmentId(Integer.valueOf(equipmentId));
            corePollingEquipment.setPlanId(planId);
            corePollingEquipment.setStatus(0);
            corePollingEquipmentMapper.insert(corePollingEquipment);
        }
    }
}
